﻿
//注册
function Reg() {
    if (this.req.responseText == "succeeded") {
        alert("恭喜你，注册成功！");
    }
    else {
        alert("注册失败");
    }
}
function UserReg(name,password,question,answer,sex,address,email,phone,sighWords) {
    if (name == "" || password == ""||question==""||answer==""||sex=="") return;
    var url = "AjaxResponse/Register.aspx?name=" + encodeURIComponent(name) +
    "&password=" + encodeURIComponent(password) +
    "&question=" + encodeURIComponent(question) +
    "&answer=" + encodeURIComponent(answer)+
    "&sex=" + encodeURIComponent(sex)+
    "&address=" + encodeURIComponent(address)+
    "&email=" + encodeURIComponent(email) +
    "&phone=" + encodeURIComponent(phone)+
    "&sighWords=" + encodeURIComponent(sighWords);
    new ContentLoader(url,  Reg);
}

function RegsterForm() {
    var FormCode = document.createElement("<div></div>");
    
    var Welcome = document.createElement("<div></div>");
    Welcome.innerHTML = "欢迎注册";
    FormCode.appendChild(Welcome);
    var tip = document.createElement("<div style='color:red;'></div>");
    tip.innerHTML = "温馨提示：带星号的为必填。";
    FormCode.appendChild(tip);
    
    var profile = document.createElement("<table style='width:300;background-color:red'></table>"); //在制表的时候，<table>和<tr>之间会被自动加上<tbody>标记，
    var name = NewRow("用户名","<input type = 'text' onchange='CheckName(this);'><span style='color:red' >*</span>");
    profile.appendChild(name);
    var password = NewRow("密码", "<input type = 'password' onchange='CheckPassword(this);'><span style='color:red'>*</span>");
    profile.appendChild(password);
    var passwordCheck = NewRow("密码确认", "<input type = 'password' onchange='CheckPasswordAgain(this);'><span style='color:red'>*</span>");
    profile.appendChild(passwordCheck);
    var sex = NewRow("性别", "<input type = 'text' value='男'  onchange='CheckSex(this);'><span style='color:red'>*</span>");
    profile.appendChild(sex);
    var address = NewRow("联系地址", "<input type = 'text'><span style='color:red'></span>");
    profile.appendChild(address);
    var email = NewRow("电子邮箱", "<input type = 'text'><span style='color:red'></span>");
    profile.appendChild(email);
    var phone = NewRow("电话/手机", "<input type = 'text'><span style='color:red'></span>");
    profile.appendChild(phone);
    var sighWords = NewRow("个性签名", "<input type='text'><span style='color:red'></span>");
    profile.appendChild(sighWords);
    var question = NewRow("密码提示问题", "<input onclick = 'QuestionList(this);' type='text' onchange='CheckQuestion(this);'><span style='color:red'>*</span>");
    profile.appendChild(question);
    var answer = NewRow("密码提示答案", "<input type='text' onchange='CheckAnswer(this);'><span style='color:red'>*</span>");
    profile.appendChild(answer);
    
    FormCode.appendChild(profile);
   
    var send = document.createElement("<button onclick = 'Register(this);'></button>");
    send.innerHTML = "注册";
    FormCode.appendChild(send);
    
    NewContentWin("新用户注册",FormCode.outerHTML);
 }
 function NewRow(leftHTML,rightHTML) {
     var row = document.createElement("<tr></tr>");
     var rowLeft = document.createElement("<td style='width:20%;background-color:white;text-align:right'></td>");
     rowLeft.innerHTML = leftHTML;
     var rowRight = document.createElement("<td  style='width:80%;background-color:white;text-align:left'></td>");
     rowRight.innerHTML = rightHTML;
     row.appendChild(rowLeft);
     row.appendChild(rowRight);
     return row;
 }

 function QuestionList(source) {
     var list = new Array("你的母校是？", "你的家乡是？", "你的外号是？", "你最难忘的一个词是？", "你的初恋是？","自定义");
     NewDropdownList(source,list,SetValue,"V","Bottom");
 }
 function SetValue(source, value) {
     if (value == "自定义") {
         source.value = "";
         source.focus();
         return;
     }
     source.value = value;
     source.focus();
     source.blur();
     }

     function Register(source) {
      if (!CheckAll(source.parentNode.childNodes[2].childNodes[0].childNodes[9].childNodes[1].childNodes[0])) return false;
     var profile = source.parentNode.childNodes[GetCurElementIndex(source) - 1];
     
     var name = profile.childNodes[0].childNodes[0].childNodes[1].childNodes[0].value;
     var password = profile.childNodes[0].childNodes[1].childNodes[1].childNodes[0].value;
     var sex = profile.childNodes[0].childNodes[3].childNodes[1].childNodes[0].value;
     var address = profile.childNodes[0].childNodes[4].childNodes[1].childNodes[0].value;
     var email = profile.childNodes[0].childNodes[5].childNodes[1].childNodes[0].value;
     var phone = profile.childNodes[0].childNodes[6].childNodes[1].childNodes[0].value;
     var sighWords = profile.childNodes[0].childNodes[7].childNodes[1].childNodes[0].value;
     var question = profile.childNodes[0].childNodes[8].childNodes[1].childNodes[0].value;
     var answer = profile.childNodes[0].childNodes[9].childNodes[1].childNodes[0].value;

     UserReg(name, password, question, answer, sex, address, email, phone, sighWords);
     getGuideItem(source.parentNode.parentNode).childNodes[1].click();
     
 }

 function CheckName(source) {
     if (source.value == "") { source.parentNode.childNodes[GetCurElementIndex(source) + 1].innerHTML = "用户名不能为空！"; return false; }
     var url = "AjaxResponse/IsNameExist.aspx?newName=" + encodeURIComponent(source.value);
     var isExist = function() {
     if (this.req.responseText == "Exist") { source.parentNode.childNodes[GetCurElementIndex(source) + 1].innerHTML = "你rp不好，用户名已存在"; return false; }
     else { source.parentNode.childNodes[GetCurElementIndex(source) + 1].innerHTML = "恭喜你！用户名可用！"; return true; }
     };
     new ContentLoader(url, isExist);
     source.parentNode.childNodes[GetCurElementIndex(source) + 1].innerHTML = "*";
     return true;
 }

 function CheckPassword(source) {
     if (source.value == "") { source.parentNode.childNodes[GetCurElementIndex(source) + 1].innerHTML = "密码不能为空！"; return false; }
     source.parentNode.childNodes[GetCurElementIndex(source) + 1].innerHTML = "*";
     return true;
 }
 function CheckPasswordAgain(source) {
     if (source.value == "") { source.parentNode.childNodes[GetCurElementIndex(source) + 1].innerHTML = "密码确认不能为空！";return false; }
     var Password = source.parentNode.parentNode.parentNode.childNodes[GetCurElementIndex(source.parentNode.parentNode) - 1].childNodes[1].childNodes[0].value;
     if (source.value != Password) { source.parentNode.childNodes[GetCurElementIndex(source) + 1].innerHTML = "前后密码不一致！";return false; }
     source.parentNode.childNodes[GetCurElementIndex(source) + 1].innerHTML = "*";
     return true;
 }

 function CheckSex(source) {
     if (source.value != "男") source.value = "女";
     return true;
  }
  function CheckEmail(source) {
      return true;
      if (source.value == "") return true;
      { source.parentNode.childNodes[GetCurElementIndex(source) + 1].innerHTML = "邮箱不正确！"; return false; }
      source.parentNode.childNodes[GetCurElementIndex(source) + 1].innerHTML = "*";
      return true;
  }
  function CheckQuestion(source) {
     if (source.value == "") { source.parentNode.childNodes[GetCurElementIndex(source) + 1].innerHTML = "密码提示问题不能为空！"; return false; }
     source.parentNode.childNodes[GetCurElementIndex(source) + 1].innerHTML = "*";
     return true;
 }

 function CheckAnswer(source) {
     if (source.value == "") { source.parentNode.childNodes[GetCurElementIndex(source) + 1].innerHTML = "密码提示答案不能为空！"; return false; }
     source.parentNode.childNodes[GetCurElementIndex(source) + 1].innerHTML = "*";
     return true;
 }

 function CheckAll(source) {
     var profile = source.parentNode.parentNode.parentNode.parentNode;
     for (var i = 0; i <10; i++) {
         if (i == 0) {
             if (!CheckName(profile.childNodes[0].childNodes[i].childNodes[1].childNodes[0])) {
                 return false;
             }
          }
          if (i == 1) {
              if (!CheckPassword(profile.childNodes[0].childNodes[i].childNodes[1].childNodes[0])) {
                  return false;
              } 
         }
         if (i == 2) {
             if (!CheckPasswordAgain(profile.childNodes[0].childNodes[i].childNodes[1].childNodes[0])) {
                 return false;
             } 
         }
         if (i == 3) {
             if (!CheckSex(profile.childNodes[0].childNodes[i].childNodes[1].childNodes[0])) {
                 return false;
             }
         } 
         if (i == 5) {
             if (!CheckEmail(profile.childNodes[0].childNodes[i].childNodes[1].childNodes[0])) {
                 return false;
             }
         }
         if (i == 8) {
             if (!CheckQuestion(profile.childNodes[0].childNodes[i].childNodes[1].childNodes[0])) {
                 return false;
             }
         }
         if (i == 9) {
             if (!CheckAnswer(profile.childNodes[0].childNodes[i].childNodes[1].childNodes[0])) {
                 return false;
             }
             break;
         }
         
     }
     return true;
 }